# -*- coding: utf-8 -*-
from datetime import timedelta
from utils.operators.spark_submit import SparkSubmitOperator

jdbcUrl='{{ var.json.oracle_bigdata03.url }}'
username='{{ var.json.oracle_bigdata03.username }}'
password='{{ var.json.oracle_bigdata03.password }}'
nowdt='{{ execution_date | cst_ds }}'
nextdt='{{ execution_date | date_add(1) | cst_ds }}'
table="yl_sms_send_detail"
env='{{ var.value.env_sync }}'
jsonpara="""{
"reader":{
"connect":{
"url":"jdbcUrlpara",
"username":"usernamepara",
"password":"passwordpara",
"driver":"oracle.jdbc.driver.OracleDriver"
},
"dbtype":"oracle",
"tableName":"yl_sms_send_detail",
"where":"(send_time >= to_date('nowdt 00:00:00','yyyy-mm-dd hh24:mi:ss') and send_time < to_date('nextdt 00:00:00','yyyy-mm-dd hh24:mi:ss')) or (RECEIPT_TIME >= to_date('nowdt 00:00:00','yyyy-mm-dd hh24:mi:ss') and RECEIPT_TIME < to_date('nextdt 00:00:00','yyyy-mm-dd hh24:mi:ss'))",
"query":"",
"splitColumn":"",
"equalitySectioning":1,
"containsnull":0,
"fetchsize":"1024",
"threadNumber":1
},
"channel":{
"filterAbnormalCharacter":0
},
"writer":{
"dbtype":"hive",
"tableName":"tablepara",
"database":"jms_ods",
"writeMode": "overwrite",
"partitionColumn":"dt",
"partitionValue":"dtvalue"},
"settting":{
"env":"envpara"}
}""".replace("jdbcUrlpara",jdbcUrl).replace("usernamepara",username).replace("passwordpara",password). \
    replace("nowdt",nowdt).replace("nextdt",nextdt). \
    replace("tablepara",table). \
    replace("envpara",env)

# from airflow.providers.apache.hdfs.sensors.web_hdfs import WebHdfsSensor
#
# jms_ods__second_package_list_hi = WebHdfsSensor(
#     pool='unlimited_pool',
#     task_id="jms_ods__second_package_list_hi",
#     filepath='/dw/hive/jms_ods.db/external/second_package_list_hi/dt={{ execution_date | hour_add(-1) | cst_hs }}',
#     execution_timeout=timedelta(hours=7),
#     email=['wangbiao@yl-scm.com'],
#     retries=2,
# )

jms_ods__yl_sms_send_detail = SparkSubmitOperator(
    task_id='jms_ods__yl_sms_send_detail',
    email=['payne.jiang@jtexpress.com','yl_bigdata@yl-scm.com'],
    name='jms_ods__yl_sms_send_detail_{{ execution_date | hour_add(1) | cst_hour }}',
    pool='oracle_tab',
    pool_slots=2,
    execution_timeout=timedelta(minutes=30),
    driver_memory='2G',
    executor_memory='4G',
    executor_cores=2,
    num_executors=3,
    conf={'spark.dynamicAllocation.enabled'                  : 'true',
          'spark.shuffle.service.enabled'                    : 'true',
          'spark.dynamicAllocation.maxExecutors'             : 4,
          'spark.dynamicAllocation.cachedExecutorIdleTimeout': 30,
          'spark.sql.sources.partitionOverwriteMode'         : 'dynamic',
          'spark.executor.memoryOverhead'                    : '1G',
          },
    java_class='com.yunlu.bigdata.jobs.synchrotool.DataSynchDriver',  # spark 主类
    application='hdfs:///scheduler/jms/spark/sync/spark_sync.jar',  # spark jar 包
    application_args=[jsonpara,],
)